/*
 * @Descripttion:
 * @version:
 * @Author: jiating
 * @Date: 2021-04-15 19:07:57
 * @LastEditors: jiating
 * @LastEditTime: 2021-04-15 19:08:50
 */
import axios from 'axios'
import 'nprogress/nprogress.css'

axios.defaults.timeout = 10000

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'


 // axios.defaults.baseURL = 'http://192.168.58.89:11109/'
axios.defaults.baseURL = 'http://192.168.72.26:11109/'
// axios.defaults.baseURL = 'http://192.168.66.148:11109/'
// axios.defaults.baseURL = process.env.VUE_APP_URL

// 请求拦截器
axios.interceptors.request.use(
  config => {
    // 请求之前读取进度条
    // NProgress.start()
    // 每次发送请求之前携带token
    let token = window.sessionStorage.getItem('token')
    if (token) {
      // 添加headers
      config.headers.token = `${token}`;
      config.headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
    }else {
      config.headers={ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    }

    return config
  },
  error => {
    return Promise.error(error)
  })

// 接收响应拦截器
axios.interceptors.response.use(response => {
  // 成功响应回来进度条拉满消失
  // NProgress.done()
  // console.log(response.data.status)
  // 判断token是否过期,以及token的是否正确
  if (response.data.code == 401) {
    sessionStorage.removeItem('token')
    sessionStorage.removeItem('level')
    sessionStorage.removeItem('href')
    sessionStorage.removeItem('userName')
    this.$router.push('/login')
  }
  return response
},
error => {
  return Promise.reject(error)
})

//
// get方法，对应get请求
export function get (url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err.data)
    })
  })
}

//  post方法，对应post请求
export function post (url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}
